package epimarket.helper;

import java.lang.reflect.*;

public class DBQuery<T extends Object> {
	private String whereStr = "";
	
	@SuppressWarnings("rawtypes")
	public String insert(T obj) {
		String query = "INSERT INTO ";
		try {
			String nameclass = "";
			Class enclosingClass = obj.getClass();
			nameclass = enclosingClass.getSimpleName().substring(0, 1).toUpperCase() + enclosingClass.getSimpleName().substring(1).toLowerCase();
			query += nameclass + "(";
			Field[] fields = enclosingClass.getDeclaredFields();
			for (int i = 0, j = 0; i < fields.length; i++){
				String name = fields[i].getName();
				query += ((j != 0) ? ", " : "");
				if (!name.equals("id")) {
					query += "`" + name + "`";
					j++;
				}
			}
			query += ") VALUES(";
			for (int i = 0, j = 0; i < fields.length; i++){
				String name = fields[i].getName();
				query += ((j != 0) ? ", " : "");
				if (!name.equals("id")) {
					fields[i].setAccessible(true);
					query += "'" + fields[i].get(obj).toString() + "'";
					j++;
				}
			}
			query += ");";
			return query;
		} catch (Exception e) {
			return "";
		}
	}

    @SuppressWarnings("rawtypes")
	public String update(T obj) {
		String query = "UPDATE ";
		try {
			String nameclass = "";
			Class enclosingClass = obj.getClass();
			nameclass = enclosingClass.getSimpleName().substring(0, 1).toUpperCase() + enclosingClass.getSimpleName().substring(1).toLowerCase();
			query += nameclass + " SET ";
			Field[] fields = enclosingClass.getDeclaredFields();
			String where_clause = "";
			for (int i = 0, j = 0; i < fields.length; i++){
				String name = fields[i].getName();
				query += ((j != 0) ? ", " : "");
				if (!name.equals("id")) {
					fields[i].setAccessible(true);
					query += "`" + name + "`=" + "'" + fields[i].get(obj).toString() + "'";
					j++;
				} else {
					fields[i].setAccessible(true);
					where_clause = " WHERE id='" + fields[i].get(obj).toString() + "'";
				}
			}
			query += where_clause + ";";
			return query;
		} catch (Exception e) {
			return "";
		}
	}

	@SuppressWarnings("rawtypes")
	public String delete(T obj) {
		String query = "DELETE FROM ";
		try {
			String nameclass = "";
			Class enclosingClass = obj.getClass();
			nameclass = enclosingClass.getSimpleName().toLowerCase();
			query += nameclass + " WHERE ";
			Field field = enclosingClass.getDeclaredField("id");
			field.setAccessible(true);
			query += "`id`=" + "'" + field.get(obj).toString() + "'";
			return query + ";";
		} catch (Exception e) {
			return "";
		}
	}
	
	@SuppressWarnings("rawtypes")
	public String find(T obj, int id) {
		String query = "SELECT * FROM ";
		Class enclosingClass = obj.getClass();
		query += enclosingClass.getSimpleName().toLowerCase() + " WHERE id=" + id + ";";
		return query;
	}
	
	@SuppressWarnings("rawtypes")
	public String all(T obj) {
		String query = "SELECT * FROM ";
		Class enclosingClass = obj.getClass();
		query += enclosingClass.getSimpleName().toLowerCase();
		if (!this.whereStr.isEmpty()) {
			query += " WHERE " + this.whereStr;
			this.whereStr = "";
		}
		return query + ";";
	}
	
	public DBQuery<T> where_eq(String field, String value) {
		if (this.whereStr.isEmpty()) {
			this.whereStr = field + "='" + value + "'";
		} else {
			this.whereStr += " AND " + field + "='" + value + "'";
		}
		return this;
	}
	
	public DBQuery<T> where_inf(String field, String value) {
		if (this.whereStr.isEmpty()) {
			this.whereStr = field + "<'" + value + "'";
		} else {
			this.whereStr += " AND " + field + "<'" + value + "'";
		}
		return this;
	}
	
	public DBQuery<T> where_sup(String field, String value) {
		if (this.whereStr.isEmpty()) {
			this.whereStr = field + ">'" + value + "'";
		} else {
			this.whereStr += " AND " + field + ">'" + value + "'";
		}
		return this;
	}
	
	public DBQuery<T> where_infeq(String field, String value) {
		if (this.whereStr.isEmpty()) {
			this.whereStr = field + "<='" + value + "'";
		} else {
			this.whereStr += " AND " + field + "<='" + value + "'";
		}
		return this;
	}
	
	public DBQuery<T> where_supeq(String field, String value) {
		if (this.whereStr.isEmpty()) {
			this.whereStr = field + ">='" + value + "'";
		} else {
			this.whereStr += " AND " + field + ">='" + value + "'";
		}
		return this;
	}
	
	public DBQuery<T> where_dif(String field, String value) {
		if (this.whereStr.isEmpty()) {
			this.whereStr = field + "!='" + value + "'";
		} else {
			this.whereStr += " AND " + field + "!='" + value + "'";
		}
		return this;
	}
}
